package day04;

import com.blackhorse.annotation.dao.AUserMapper;
import com.blackhorse.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Mybatis注解配置时一级缓存和xml配置时完全相同
 */
public class Demo5_AnnotationCache {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void init() {
        try {
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(in);
            sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void destory() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    //测试一级缓存，mybatis的一级缓存是sqlSession的缓存，其内部有一个map结构
    //使用clearCache方法可以手动清除一级缓存
    @Test
    public void testFristLevelCahce1() {
        AUserMapper userMapperr = sqlSession.getMapper(AUserMapper.class);
        User user1 = userMapperr.findById(2);
        //sqlSession.clearCache();//清除一级缓存
        User user2 = userMapperr.findById(2);
        System.out.print("两次查询user的内存地址是否相同：");
        System.out.println(user1 == user2);
    }

    //测试一级缓存，sqlSession一旦关闭，一级缓存消失
    @Test
    public void testFristLevelCahce2() {
        AUserMapper userMapperr = sqlSession.getMapper(AUserMapper.class);
        User user1 = userMapperr.findById(2);
        sqlSession.close();//关闭sqlSession

        sqlSession = sqlSessionFactory.openSession();//再次开启sqlSession
        userMapperr = sqlSession.getMapper(AUserMapper.class);
        User user2 = userMapperr.findById(2);
        System.out.print("两次查询user的内存地址是否相同：");
        System.out.println(user1 == user2);
    }

    //测试一级缓存，sqlSession一旦执行增删改操作或提交事务，一级缓存被清空
    @Test
    public void testFristLevelCahce3() {
        AUserMapper userMapperr = sqlSession.getMapper(AUserMapper.class);
        User user1 = userMapperr.findById(2);
        System.out.println(user1);

        user1.setBirthday(new Date());
        userMapperr.updateUser(user1);//更新用户
        sqlSession.commit();//提交事务

        User user2 = userMapperr.findById(2);
        System.out.println(user2);
        System.out.print("两次查询user的内存地址是否相同：");
        System.out.println(user1 == user2);
    }

    //注解配置
    //测试二级缓存，二级缓存是SqlSessionFactory的缓存，同一个工厂生产的SqlSession共享二级缓存
    //1.开启二级缓存总开关 在SqlMapConfig.xml配置
    //2.在Mapper.java类上添加@CacheNameSpace注解
    @Test
    public void testSecondLevelCahce() {
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        AUserMapper userMapper1 = sqlSession1.getMapper(AUserMapper.class);
        User user1 = userMapper1.findById(2);
        sqlSession1.close();//清除一级缓存

        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        AUserMapper userMapper2 = sqlSession2.getMapper(AUserMapper.class);
        User user2 = userMapper2.findById(2);
        sqlSession2.close();//清除一级缓存

        //可以观察sql执行次数和cache hit ratio判断是否使用了二级缓存
        System.out.print("两次查询user的内存地址是否相同：");
        System.out.println(user1 == user2);//二级缓存中存储的是数据而不是对象，因此这里返回false
    }
}
